package es.mgil.blog;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class SignBlogServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(SignBlogServlet.class
			.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		String title = req.getParameter("title");
		String content = req.getParameter("content");
		Date date = new Date();
		Mensaje mensaje = new Mensaje(user == null ? null : user, title,
				content, date);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(mensaje);
		} finally {
			pm.close();
		}

		log.warning(content);
		resp.sendRedirect("/blog");
		resp.setCharacterEncoding("utf-8");
	}
}
